package com.diyunapp.xilinapp.fragments;


import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.login.LoginActivity;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.me.AccountSafeActivity;
import com.diyunapp.xilinapp.me.AddressActivity;
import com.diyunapp.xilinapp.me.DaiJinQuanActivity;
import com.diyunapp.xilinapp.me.MyCollectActivity;
import com.diyunapp.xilinapp.me.MyMessageActivity;
import com.diyunapp.xilinapp.me.SettingsActivity;
import com.diyunapp.xilinapp.me.SharedActivity;
import com.diyunapp.xilinapp.me.SumMoneyActivity;
import com.diyunapp.xilinapp.me.UserInfoActivity;
import com.diyunapp.xilinapp.me.XiLinCoinActivity;
import com.diyunapp.xilinapp.me.integralActivity;
import com.diyunapp.xilinapp.myindent.ComplaintsaListActivity;
import com.diyunapp.xilinapp.myindent.FootmarkActivity;
import com.diyunapp.xilinapp.myindent.RefundSalesActivity;
import com.diyunapp.xilinapp.myorders.MyOrdersActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nuttysoft.nutand.views.RoundImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment {


    private static SharedPreferences sp;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.setting_tv)
    TextView settingTv;
    @Bind(R.id.rel_topbar)
    RelativeLayout relTopbar;
    @Bind(R.id.user_icon)
    RoundImageView userIcon;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.index_sum_money)
    TextView indexSumMoney;
    @Bind(R.id.lin_sum_money)
    LinearLayout linSumMoney;
    @Bind(R.id.index_sum_jifen)
    TextView indexSumJifen;
    @Bind(R.id.lin_jifen)
    LinearLayout linJifen;
    @Bind(R.id.index_sum_xilinbi)
    TextView indexSumXilinbi;
    @Bind(R.id.lin_xilinbi)
    LinearLayout linXilinbi;
    @Bind(R.id.lin_1)
    LinearLayout lin1;
    @Bind(R.id.image_nopay)
    ImageView imageNopay;
    @Bind(R.id.wait_pay)
    RelativeLayout waitPay;
    @Bind(R.id.image_noreceive)
    ImageView imageNoreceive;
    @Bind(R.id.wait_send)
    RelativeLayout waitSend;
    @Bind(R.id.image_receive)
    ImageView imageReceive;
    @Bind(R.id.wait_receive)
    RelativeLayout waitReceive;
    @Bind(R.id.image_evaluate)
    ImageView imageEvaluate;
    @Bind(R.id.wait_evaluate)
    RelativeLayout waitEvaluate;
    @Bind(R.id.lin_2)
    LinearLayout lin2;
    @Bind(R.id.line_1)
    TextView line1;
    @Bind(R.id.lin_dd)
    LinearLayout linDd;
    @Bind(R.id.bg_1)
    View bg1;
    @Bind(R.id.textview_zhye)
    TextView textviewZhye;
    @Bind(R.id.lin_ye)
    LinearLayout linYe;
    @Bind(R.id.line_2)
    TextView line2;
    @Bind(R.id.textview_wdjf)
    TextView textviewWdjf;
    @Bind(R.id.lin_jf)
    LinearLayout linJf;
    @Bind(R.id.line_3)
    TextView line3;
    @Bind(R.id.textview_wddjq)
    TextView textviewWddjq;
    @Bind(R.id.lin_djq)
    LinearLayout linDjq;
    @Bind(R.id.line_4)
    TextView line4;
    @Bind(R.id.textview_wdxlb)
    TextView textviewWdxlb;
    @Bind(R.id.lin_xlb)
    LinearLayout linXlb;
    @Bind(R.id.bg_2)
    View bg2;
    @Bind(R.id.textview_wdsc)
    TextView textviewWdsc;
    @Bind(R.id.lin_sc)
    LinearLayout linSc;
    @Bind(R.id.line_sc)
    TextView lineSc;
    @Bind(R.id.textview_wdzj)
    TextView textviewWdzj;
    @Bind(R.id.lin_zj)
    LinearLayout linZj;
    @Bind(R.id.line_zj)
    TextView lineZj;
    @Bind(R.id.textview_shdz)
    TextView textviewShdz;
    @Bind(R.id.lin_dz)
    LinearLayout linDz;
    @Bind(R.id.line_dz)
    TextView lineDz;
    @Bind(R.id.textview_wdxx)
    TextView textviewWdxx;
    @Bind(R.id.lin_xx)
    LinearLayout linXx;
    @Bind(R.id.line_xx)
    TextView lineXx;
    @Bind(R.id.textview_zhaq)
    TextView textviewZhaq;
    @Bind(R.id.lin_aq)
    LinearLayout linAq;
    @Bind(R.id.line_aq)
    TextView lineAq;
    @Bind(R.id.textview_thjhh)
    TextView textviewThjhh;
    @Bind(R.id.lin_th)
    LinearLayout linTh;
    @Bind(R.id.line_th)
    TextView lineTh;
    @Bind(R.id.textview_tj)
    TextView textviewTj;
    @Bind(R.id.lin_tj)
    LinearLayout linTj;
    @Bind(R.id.home_products_srl)
    SwipeRefreshLayout homeProductsSrl;

    public String data;
    @Bind(R.id.pay_data)
    TextView payData;
    @Bind(R.id.send_data)
    TextView sendData;
    @Bind(R.id.shou_data)
    TextView shouData;
    @Bind(R.id.ping_data)
    TextView pingData;
    private Bitmap myHeadPic_bm;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View layout = inflater.inflate(R.layout.fragment_mine, null);
        ButterKnife.bind(this, layout);
        if (MainActivity.myLogin.isLoginState()) {
            init();
            getAccountInfo();
            initView();
        } else {
            userName.setText("未登录");
            userIcon.setImageResource(R.drawable.user_yong);
        }

        return layout;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if(!hidden)
        {
            if (MainActivity.myLogin.isIfneed_fresh() && MainActivity.myLogin.isLoginState()) {
                getAccountInfo();
            }
            if(MainActivity.myLogin.isLoginState())
            {
                initView();
            }
        }
        super.onHiddenChanged(hidden);
    }

    private void init() {
        homeProductsSrl.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        homeProductsSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                if (MainActivity.myLogin.isLoginState()) {
                    init();
                    getAccountInfo();
                    initView();
                }
            }
        });
    }

    private void initView() {

        getPayData("daifukuan");
        getSendData("daifahuo");
        getShouData("daishouhuo");
        getPingData("daipingjia");
    }

    @OnClick({R.id.setting_tv, R.id.user_icon, R.id.lin_sum_money, R.id.lin_jifen, R.id.lin_xilinbi,
            R.id.wait_pay, R.id.wait_send, R.id.wait_receive, R.id.wait_evaluate,
            R.id.lin_dd, R.id.lin_ye, R.id.lin_jf, R.id.lin_djq, R.id.lin_xlb, R.id.lin_sc,
            R.id.lin_zj, R.id.lin_dz, R.id.lin_xx, R.id.lin_aq, R.id.lin_th, R.id.lin_tj,R.id.lin_ts})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_tv:
                Intent setInt = new Intent(getActivity(), SettingsActivity.class);
                startActivityForResult(setInt, 1);
                break;
            case R.id.user_icon:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入设置页面
                {
                    Intent setInt1 = new Intent(getActivity(), UserInfoActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.wait_pay:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入待付款页面
                {
                    myOrdersActivity(1);

                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);

                }

                break;
            case R.id.wait_send:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入代发货页面
                {
                    myOrdersActivity(2);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.wait_receive:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入待收货页面
                {
                    myOrdersActivity(3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.wait_evaluate:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入待评价页面
                {
                    myOrdersActivity(4);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_dd:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入订单页面
                {
                    myOrdersActivity(0);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_sum_money:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入账户页面
                {
                    Intent setInt1 = new Intent(getActivity(), SumMoneyActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_ye:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入账户页面
                {
                    Intent setInt1 = new Intent(getActivity(), SumMoneyActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_jifen:
                if (MainActivity.myLogin.isLoginState()) {
                    Intent intent1 = new Intent();
                    intent1.setClass(getActivity(), integralActivity.class);
                    intent1.putExtra("member_points", indexSumJifen.getText().toString().trim());
                    startActivity(intent1);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_jf:
                if (MainActivity.myLogin.isLoginState()) {
                    Intent intent1 = new Intent();
                    intent1.setClass(getActivity(), integralActivity.class);
                    intent1.putExtra("member_points", indexSumJifen.getText().toString().trim());
                    startActivity(intent1);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_xilinbi:
                if (MainActivity.myLogin.isLoginState()) {
                    Intent intent2 = new Intent();
                    intent2.setClass(getActivity(), XiLinCoinActivity.class);
                    intent2.putExtra("member_xlb", indexSumXilinbi.getText().toString().trim());
                    startActivity(intent2);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }

                break;
            case R.id.lin_xlb:
                if (MainActivity.myLogin.isLoginState()) {
                    Intent intent2 = new Intent();
                    intent2.setClass(getActivity(), XiLinCoinActivity.class);
                    intent2.putExtra("member_xlb", indexSumXilinbi.getText().toString().trim());
                    startActivity(intent2);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_djq:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入代金券页面
                {
                    Intent setInt1 = new Intent(getActivity(), DaiJinQuanActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_dz:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入地址页面
                {
                    Intent setInt1 = new Intent(getActivity(), AddressActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_xx:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入消息页面
                {
                    Intent setInt1 = new Intent(getActivity(), MyMessageActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_aq:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入安全页面
                {
                    Intent setInt1 = new Intent(getActivity(), AccountSafeActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }

                break;
            case R.id.lin_th:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入退货页面
                {
                    Intent setInt1 = new Intent(getActivity(), RefundSalesActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_zj:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入足迹页面
                {
                    Intent setInt1 = new Intent(getActivity(), FootmarkActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_sc:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入收藏页面
                {
                    Intent setInt1 = new Intent(getActivity(), MyCollectActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_ts:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入投诉页面
                {
                    Intent setInt1 = new Intent(getActivity(), ComplaintsaListActivity.class);
                    startActivityForResult(setInt1, 4);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;
            case R.id.lin_tj:
                if (MainActivity.myLogin.isLoginState()) //用户已登录，进入分享页面
                {
                    Intent setInt1 = new Intent(getActivity(), SharedActivity.class);
                    startActivityForResult(setInt1, 3);
                } else {
                    Intent logInt = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(logInt, 2);
                }
                break;

            default:
                break;

        }
    }

    private void myOrdersActivity(int state) {
        Intent ordersInt = new Intent(getActivity(), MyOrdersActivity.class);
        ordersInt.putExtra("OrderState", state);
        startActivity(ordersInt);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getAccountInfo() {
        final String url = "http://www.xilingou.com/mobile/index.php?act=goods&op=member_info&username=" + MainActivity.myLogin.getUsername();
        Log.i("zhangxiaoxidebug", "getAccountInfo url=" + url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.GET, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        homeProductsSrl.setRefreshing(false);
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        MainActivity.myLogin.setIfneed_fresh(false);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");

                            if (result == 200) {
                                JSONObject recordObject = resultObject.optJSONObject("date");
                                String member_name = recordObject.optString("member_name");
                                String available_predeposit = recordObject.optString("available_predeposit");
                                String member_points = recordObject.optString("member_points");
                                String member_xlb = recordObject.optString("member_xlb");
                                String member_avatar = recordObject.optString("member_avatar");

                                MainActivity.myLogin.setHeadimge(member_avatar);
//                                ImageLoader.getInstance().displayImage(member_avatar,userIcon, FormatConfirm.getOption());
//                                Picasso.with(getActivity())
//                                        .load(member_avatar)
//                                        .resize(64, 64)
//                                        .centerCrop()
//                                        .placeholder(R.drawable.yong)
//                                        .error(R.drawable.yong)
//                                        .into(userIcon);
                                if (MainActivity.myLogin.isUpdate_head() || MainActivity.myLogin.isGet_head()) {
                                    userIcon.setImageBitmap(MainActivity.myLogin.getTb_headPic());
                                } else {
//                                    userIcon.setImageBitmap(returnBitMap(member_avatar));
                                    if(member_avatar=="http://www.xilingou.com/data/upload/shop/avatar/"||member_avatar.equals("http://www.xilingou.com/data/upload/shop/avatar/")){
                                        userIcon.setImageResource(R.drawable.user_yong);
                                    }else if (member_avatar!="http://www.xilingou.com/data/upload/shop/avatar/"){
                                        GetPictureTask myPicTask = new GetPictureTask();
                                        myPicTask.execute(member_avatar);
                                    }

//                                    Picasso.with(getActivity())
//                                            .load(member_avatar)
//                                            .resize(64, 64)
//                                            .centerCrop()
//                                            .placeholder(R.drawable.yong)
//                                            .error(R.drawable.yong)
//                                            .into(userIcon);
                                }
//                                String path=member_avatar;
//                                URL url = new URL(path);
//                                ImageUtils.onLoadImage(url, new ImageUtils.OnLoadImageListener() {
//                                    @Override
//                                    public void OnLoadImage(Bitmap bitmap, String bitmapPath) {
//                                        // TODO Auto-generated method stub
//                                        if(bitmap!=null){
//                                            userIcon.setImageBitmap(bitmap);
//                                        }
//                                    }
//                                });
                                userName.setText(member_name);
                                indexSumMoney.setText("￥" + available_predeposit);
                                indexSumJifen.setText(member_points);
                                indexSumXilinbi.setText(member_xlb);
                            } else {

                                Toast.makeText(getActivity(), "账户信息读取失败，请检查您的网络！", Toast.LENGTH_SHORT).show();

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "账户信息读取失败，请联系客服处理！", Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        homeProductsSrl.setRefreshing(false);
                        Toast.makeText(getActivity(), "账户信息读取失败，请检查您的网络！", Toast.LENGTH_SHORT).show();

                    }
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //执行退出当前账户后，返回我的页面，并清空相关信息
        if (requestCode == 1 && resultCode == 1) {
            //用户点击退出当前账户后，首先清空所有的设置，再进入登录界面
            SharedPreferences user_info = getActivity().getSharedPreferences("xilinshangchengSP", 0);
            user_info.edit().putString("username", "").commit();

            user_info.edit().putString("member_id", "").commit();

            user_info.edit().putBoolean("haveLogin", false).commit();

            user_info.edit().putString("key", "").commit();

            MainActivity.myLogin.setUsername("");
            MainActivity.myLogin.setLoginkey("");
            MainActivity.myLogin.setLoginState(false);
            MainActivity.myLogin.setMemberid("");

            userName.setText("未登录");
            indexSumMoney.setText("￥0");
            indexSumJifen.setText("0");
            indexSumXilinbi.setText("0");
            userIcon.setImageResource(R.drawable.user_yong);
            sendData.setVisibility(View.GONE);
            payData.setVisibility(View.GONE);
            shouData.setVisibility(View.GONE);
            pingData.setVisibility(View.GONE);
        }

        //用户登录成功后，退回我的页面，并查询更新账户信息
        if (requestCode == 2) {
            if (resultCode == 1) {
                getAccountInfo();
                initView();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void getPayData(String type) {
        String url = "http://www.xilingou.com/mobile/index.php?act=member_order&op=order_list&key="
                + MainActivity.myLogin.getLoginkey() + "&type=" + type + "";
        Log.d("zhangxiaoxidebug", "11111" + url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        homeProductsSrl.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if (result == 200) {
                                JSONObject datas = resultObject.optJSONObject("datas");
                                JSONArray order_group_list = datas.optJSONArray("order_group_list");
                                int i = order_group_list.length();
                                Log.i("xiaogalaxiede1","iiii+"+i);
                                String j= String.valueOf(order_group_list.length());
                                if (i!=0){
                                    payData.setVisibility(View.VISIBLE);
                                payData.setText(j);
                                Log.d("zhangxiaoxidebug", "11111" + i);
                                } else if (i==0) {
                                    payData.setVisibility(View.GONE);
                                }
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
//                          toast("查询失败");
                            homeProductsSrl.setRefreshing(false);
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        homeProductsSrl.setRefreshing(false);
//                      toast("请求失败");
                    }
                });
    }
    public void getSendData(String type) {
        String url = "http://www.xilingou.com/mobile/index.php?act=member_order&op=order_list&key="
                + MainActivity.myLogin.getLoginkey() + "&type=" + type + "";
        Log.d("zhangxiaoxidebug", "11111" + url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        homeProductsSrl.setRefreshing(false);
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if (result == 200) {
                                JSONObject datas = resultObject.optJSONObject("datas");
                                JSONArray order_group_list = datas.optJSONArray("order_group_list");
                                int i = order_group_list.length();
                                Log.i("xiaogalaxiede2","iiii+"+i);
                                String j= String.valueOf(order_group_list.length());
                                if (i!=0){
                                    sendData.setVisibility(View.VISIBLE);
                                    sendData.setText(j);
                                    Log.d("zhangxiaoxidebug", "11111" + i);
                                }else if(i==0) {
                                    sendData.setVisibility(View.GONE);
                                }
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
//                          toast("查询失败");
                            homeProductsSrl.setRefreshing(false);
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
//                      toast("请求失败");
                    }
                });
    }
    public void getShouData(String type) {
        String url = "http://www.xilingou.com/mobile/index.php?act=member_order&op=order_list&key="
                + MainActivity.myLogin.getLoginkey() + "&type=" + type + "";
        Log.d("zhangxiaoxidebug", "11111" + url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        homeProductsSrl.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if (result == 200) {
                                JSONObject datas = resultObject.optJSONObject("datas");
                                JSONArray order_group_list = datas.optJSONArray("order_group_list");
                                int i = order_group_list.length();
                                Log.i("xiaogalaxiede3","iiii+"+i);
                                String j= String.valueOf(order_group_list.length());
                                if (i!=0){
                                    shouData.setVisibility(View.VISIBLE);
                                    shouData.setText(j);
                                    Log.d("zhangxiaoxidebug", "11111" + i);
                                }else {
                                    shouData.setVisibility(View.GONE);
                                }
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
//                          toast("查询失败");
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        homeProductsSrl.setRefreshing(false);
//                      toast("请求失败");
                    }
                });
    }
    public void getPingData(String type) {
        String url = "http://www.xilingou.com/mobile/index.php?act=member_order&op=order_list&key="
                + MainActivity.myLogin.getLoginkey() + "&type=" + type + "";
        Log.d("zhangxiaoxidebug", "44444444" + url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        homeProductsSrl.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if (result == 200) {
                                JSONObject datas = resultObject.optJSONObject("datas");
                                JSONArray order_group_list = datas.optJSONArray("order_group_list");
                                int i = order_group_list.length();
                                String j= String.valueOf(order_group_list.length());
                                Log.i("xiaogalaxiede4","iiii+"+i);
                                if (i!=0){
                                    pingData.setVisibility(View.VISIBLE);
                                    pingData.setText(j);
                                    Log.d("zhangxiaoxidebug", "11111" + i);
                                }else {
                                    pingData.setVisibility(View.GONE);
                                }
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
//                          toast("查询失败");
                            homeProductsSrl.setRefreshing(false);
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
//                      toast("请求失败");
                        homeProductsSrl.setRefreshing(false);
                    }
                });
    }
    private class GetPictureTask extends AsyncTask<String, String, Integer> {
        @Override
        protected Integer doInBackground(String... params) {
            String pictureUrl = params[0];
            URL imgUrl = null;
            try {
                imgUrl = new URL(pictureUrl);
                HttpURLConnection conn = (HttpURLConnection)imgUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();
                myHeadPic_bm = BitmapFactory.decodeStream(is);
                is.close();
                return 1;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return 10;
            }catch(IOException e){
                e.printStackTrace();
                return 11;
            }
        }
        @Override
        protected void onPostExecute(Integer result) {
            switch (result) {
                case 1:
                    userIcon.setImageBitmap(myHeadPic_bm);
                    break;
                case 10:
                    break;
                case 11:
                    break;
                default:
                    break;
            }
            super.onPostExecute(result);
        }
    }
}
